﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Entity.Table;
using SqlHelper;
using System.Data;
using System.Data.SqlClient;

namespace DAL
{
    public class StrikeBalanceDatabase
    {
        /// <summary>
        /// 验证数据库中是否存在要冲账的交费流水
        /// </summary>
        /// <param name="bankWater"></param>
        /// <param name="gasUserID"></param>
        /// <returns></returns>
        public static bool IsExistTradeFlow(string bankWater, string gasUserID,string payWay,out int isWriteCard)
        {
            int count = 0;
            isWriteCard = 0;
            SqlConnection conn = null;
            SqlParameter[] paramList = new SqlParameter[] { DbHelper.CreateCommandParameter("@GasUserID",SqlDbType.VarChar,gasUserID),
                                                            DbHelper.CreateCommandParameter("@BankWaterNumber",SqlDbType.VarChar,bankWater),
                                                            DbHelper.CreateCommandParameter("@PayWay",SqlDbType.Int,payWay)
                                                           };
            try
            {
                conn = DbHelper.CreateDbConnection(DbHelper.DbConnectionString);
                conn.Open();
                DataTable dt = DbHelper.GetTableByProcedure("NXS_CheckTradeFlow", paramList, conn);
                count = (int)dt.Rows[0][0];
                isWriteCard = dt.Rows[0][1] == DBNull.Value ? 0 : Convert.ToInt32(dt.Rows[0][1]);
            }
            finally
            {
                conn.Close();
            }
            return count > 0;
        }

        /// <summary>
        /// 冲正业务
        /// </summary>
        /// <param name="strike">冲正数据实体</param>
        /// <returns></returns>
        public static bool PerformStrikeBalance(MStrikeBalance strike,out string payMoney)
        {
            bool ret = true;
            SqlConnection conn = null;
            payMoney = "0";
            SqlParameter[] paramList = new SqlParameter[] {
                                                            DbHelper.CreateCommandParameter("@PaymentType",SqlDbType.Int,Convert.ToInt32(strike.PaymentType)),
                                                            DbHelper.CreateCommandParameter("@BankWaterID",SqlDbType.VarChar,strike.BankWaterID),
                                                            DbHelper.CreateCommandParameter("@UserID",SqlDbType.VarChar,strike.GasUserID),
                                                            DbHelper.CreateCommandOutputParameter("@PayMoney",SqlDbType.Decimal,12),
                                                            DbHelper.CreateCommandOutParameter("@return",SqlDbType.Int)
                                                           };
            try
            {
                conn = DbHelper.CreateDbConnection(DbHelper.DbConnectionString);
                conn.Open();
                DbHelper.ExecuteNonQueryByProc("NXS_StrikeBalance", paramList, conn);
                ret = ((int)paramList[4].Value) > 0;
                payMoney = paramList[3].Value.ToString();
            }
            catch (SqlException ex)
            {
                ret = false;
            }
            finally
            {
                conn.Close();
            }
            return ret;
        }
    }
}
